//app.js
App({
  post: function (url, data = {}) {
    var promise = new Promise((resolve, reject) => {

      wx.request({
        url: `https://wx.zhonghuazhengxin.com/api/${url}`,
        data: data,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          resolve(res)
        },
        fail: res => {
          reject(res)
          console.log(res)
        }
      })

    }).catch(err => {
      throw err
    })
    return promise
  },

  //多张图片上传
  uploadImg(data, imgList) {
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    let list = imgList
    wx.uploadFile({
      url: 'https://wx.zhonghuazhengxin.com/api/Upload/pic',
      filePath: data[i],
      name: 'file',
      formData: null,
      success: (resp) => {
        success++;
        console.log(JSON.parse(resp.data))
        console.log(i);
        let img = JSON.parse(resp.data)
        list.push(img.result_data)
        //这里可能有BUG，失败也会执行这里
      },
      fail: (res) => {
        fail++;
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        console.log(i);
        i++;
        if (i == data.length) { //当图片传完时，停止调用 
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
        } else { //若图片还没有传完，则继续调用函数
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadImg(data);
        }
        if (list.length >= 2) {
          list = list.toString().replace(',', '|')
        } else {
          list = list.toString()
        }
      }
    });
  },


  onLaunch: function () {


  },

  globalData: {
    userInfo: null,
  }
})
